#include "IsUserReged.h"
#include "db/DBManager.h"

#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QJsonObject>
#include <QSqlError>

void IsUserReged::handleSession()
{
    qDebug() << "IsUserReged requestUrlQuery: " << m_session->requestUrlQuery();

    QMap<QString, QString> mapQuery = m_session->requestUrlQuery();

    QString strOpenId = mapQuery.value("openid");

    QSqlDatabase db = DBManager::database("core_config");

    QString strSql = QString("select * from user_info where szOpenid='%1'").arg(strOpenId);


    QSqlQuery query(db);

    if(!query.exec(strSql))
    {
        qDebug()<<"exec select user_info error, "<<query.lastError().text();
        return;
    }

    QVariantMap mapUserInfo;
    while(query.next())
    {
        QSqlRecord record = query.record();
        for(int i=0; i<record.count(); i++)
        {
            mapUserInfo.insert(record.fieldName(i), record.value(i));
        }
    }


    QVariantMap mapResult;


    if (mapUserInfo.isEmpty())
    {
        mapResult.insert("result", "no");
    }
    else
    {
        mapResult.insert("result", "yes");
        mapResult.insert("userType", mapUserInfo.value("szType"));
        mapResult.insert("userPhone", mapUserInfo.value("szPhone"));
    }
    m_session->replyJsonObject(QJsonObject::fromVariantMap(mapResult));
}
